package sword.chapter12Sort;

import java.util.PriorityQueue;

/**
 * 数组中第k大的数字
 *
 * 解法1 小顶堆
 * 解法2 快速排序
 *
 * @author K
 * @date 2022-01-11 22:32
 */
public class S76FindKthLargest {
    public static int s76(int[] nums, int k) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<>((o1, o2) -> o1 -o2);
        for (int num : nums) {
            if (minHeap.size() < k) {
                minHeap.offer(num);
            } else {
                if (minHeap.peek() < num) {
                    minHeap.poll();
                    minHeap.offer(num);
                }
            }
        }

        return minHeap.peek();
    }

    public static void main(String[] args) {
        int[] nums = new int[]{3, 1, 2, 4, 5, 5, 6};
        int result = s76(nums, 3);
        System.out.println();
    }

}
